// pages/components/herb-select/index.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isAll: {
      type: Boolean,
      value: false
    },
    openSelectHerb: {
      type: Boolean,
      value: false,
      observer: function (value) {
        if (value) {
          this.setData({
            selectHerbDone: false,
            tempSelectHerb: null,
            selectHerbSearch: ''
          });
          this.getHerbItemList();
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    selectHerbDone: false,
    tempSelectHerb: null,
    selectHerbSearch: '',
    herbPage: {
      page: 1,
      limit: 10,
      count: 0,
      showPage: false
    },
    herbItemList: [], //可选的药品列表
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onHerbPopUpSearchChange(e) {
      let that = this;
      that.setData({
        selectHerbSearch: e.detail
      });
    },
    onHerbPopUpSearch() {
      this.getHerbItemList();
    },
    herbPrevPage: function (event) {
      let that = this;
      let herbPage = this.data.herbPage;
      if (herbPage.page <= 1) {
        return false;
      }
      that.setData({
        'herbPage.page': herbPage.page - 1
      });
      this.getHerbItemList();
    },
    herbNextPage: function (event) {
      let that = this;
      let herbPage = this.data.herbPage;
      if (herbPage.page > herbPage.count / herbPage.limit) {
        return true;
      }
      that.setData({
        'herbPage.page': herbPage.page + 1
      });
      this.getHerbItemList();
    },
    doneSelectHerb() {
      if (this.data.selectHerbDone === false) {
        return false;
      }
      let that = this;

      this.triggerEvent('callback', that.data.tempSelectHerb)
      this.resetSelectStatus();
      this.setData({
        openSelectHerb: false,
        tempSelectHerb: null
      });
    },
    //重置选中状态
    resetSelectStatus() {
      let herbItemList = this.data.herbItemList.map(item => {
        item.selected = false;
        return item;
      })
      this.setData({
        herbItemList: herbItemList
      });
    },
    cancelSelectHerb() {
      this.setData({
        openSelectHerb: false
      });
    },
    getHerbItemList() {
      let that = this;
      util.request(this.data.isAll ? api.HerbSelectList : api.HerbAuthSelectList, {
        searchContent: that.data.selectHerbSearch,
        page: that.data.herbPage.page,
        limit: that.data.herbPage.limit
      }).then(function (res) {
        if (res.errno === 0) {
          that.setData({
            herbItemList: res.data.list,
            'herbPage.show': true,
            'herbPage.count': res.data.total
          });
        }
      });
    },
    selectHerb(event) {
      let that = this;
      let herbIndex = event.target.dataset.herbIndex;
      let herbItem = that.data.herbItemList[herbIndex];

      let herbItemList = that.data.herbItemList.map(item => {
        //标记已选择的
        if (herbItem.genericName == item.genericName && herbItem.describe == item.describe) {
          item.selected = true;
        } else {
          item.selected = false;
        }
        return item;
      })

      this.setData({
        herbItemList: herbItemList,
        tempSelectHerb: herbItem,
        selectHerbDone: true
      })
    },
  }
})